package com.willren.nutrss;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.willren.http.HttpRequestEx;
import com.willren.http.HttpRequestEx.HttpRequestExInterface;
import com.willren.http.HttpRequestEx.HttpType;

public class GoogleReaderApi {

	public interface GoogleReaderApiInterface {
		public void googleReadDataReceived(boolean result);
	}

	static private GoogleReaderApi instance;

	static public GoogleReaderApi getInstance(String username, String password) {
		if (null == instance) {
			instance = new GoogleReaderApi(username, password);
		}
		return instance;
	}

	private String username;
	private String password;

	private String loginSID;
	private String loginLSID;
	private String loginAuth;

	private GoogleReaderApi(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public boolean isLogin() {
		return ((null != loginSID) && (null != loginLSID) && (null != loginAuth));
	}

	public boolean reqLogin(GoogleReaderApiInterface callback) {
		boolean ret = false;
		final GoogleReaderApiInterface finalCallback = callback;

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		if (null != params) {
			params.add(new BasicNameValuePair("service", "reader"));
			params.add(new BasicNameValuePair("Email", username));
			params.add(new BasicNameValuePair("Passwd", password));
			params.add(new BasicNameValuePair("source", "mobilescroll"));

			HttpRequestEx req = new HttpRequestEx(HttpType.HT_GET,
					"https://www.google.com/accounts/ClientLogin", null, params);
			if (null != req) {
				ret = req.send(new HttpRequestExInterface() {
					@Override
					public void httpDataReceived(int code, String response) {
						Log.i("http", "### reqLogin ###");
						Log.i("http", "code: " + code);
						Log.i("http", "response: " + response);

						if (null != finalCallback) {
							if (200 == code) {
								parseLoginResponse(response);
								finalCallback.googleReadDataReceived(true);
							} else {
								finalCallback.googleReadDataReceived(false);
							}
						}
					}
				});
			}
		}

		return ret;
	}

	public boolean reqFeeds(GoogleReaderApiInterface callback) {
		boolean ret = false;
		final GoogleReaderApiInterface finalCallback = callback;

		List<NameValuePair> headers = new ArrayList<NameValuePair>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		if (null != headers && null != params) {
			headers.add(new BasicNameValuePair("Authorization",
					"GoogleLogin auth=" + loginAuth));
			params.add(new BasicNameValuePair("output", "xml"));

			HttpRequestEx req = new HttpRequestEx(HttpType.HT_GET,
					"https://www.google.com/reader/api/0/subscription/list",
					headers, params);
			if (null != req) {
				ret = req.send(new HttpRequestExInterface() {
					@Override
					public void httpDataReceived(int code, String response) {
						Log.i("http", "### reqFeeds ###");
						Log.i("http", "code: " + code);
						Log.i("http", "response: " + response);

						if (null != finalCallback) {
							if (200 == code) {
								finalCallback.googleReadDataReceived(true);
							} else {
								finalCallback.googleReadDataReceived(false);
							}
						}
					}
				});
			}
		}

		return ret;
	}

	private void parseLoginResponse(String response) {
		try {
			BufferedReader is = new BufferedReader(new InputStreamReader(
					new ByteArrayInputStream(response.getBytes())));
			if (null != is) {
				while (true) {
					String line = is.readLine().trim();
					if (null != line) {
						if (0 == line.indexOf("SID")) {
							loginSID = line.substring(4);
						} else if (0 == line.indexOf("LSID")) {
							loginLSID = line.substring(5);
						} else if (0 == line.indexOf("Auth")) {
							loginAuth = line.substring(5);
						}
					} else {
						break;
					}
				}
			}
		} catch (Exception e) {
		}
	}
}
